/*
 *  $Id: GamePhases.cs 1189 2010-11-12 14:40:06Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

using AgateLib.DisplayLib;
using AgateLib.Geometry;
using AgateLib.InputLib;

using Ragima.Helper;

namespace Ragima {
    public enum GamePhaseIcon: int {
        None = -1,
        SwordAndShield = 0,
        Defenders = 1,
        Swords = 2,
        Attacker = 3,
        SwordsInSheath = 4,
    }

    public class GamePhases: Region {
        private static UI.Sprite _phasesSprite;
        public static UI.Sprite PhasesSprite { get { return _phasesSprite ?? (_phasesSprite = Data.GamePhases.GetSprite("phases")); } }

        private static readonly Color Background = 0x313131.ToColor();

        private GamePhaseIcon _current, _next, _next2;
        private int _step, _maxStep;
        private Hint _hint;

        public GamePhases(): base(new Rectangle(Constants.GamePhasePos, PhasesSprite.Descriptor.Size)) {
            HotKey = KeyCode.Space;
            _current = GamePhaseIcon.None;
            _next = GamePhaseIcon.None;
            _next2 = GamePhaseIcon.None;
            _step = 0;
            _maxStep = PhasesSprite.Descriptor.Size.Height;
            _hint = new TextHint(Rect, Resources.Game.EndPhaseHint.GetText());
        }

        public override Hint GetHint() {
            return _hint;
        }

        public void Change(GamePhaseIcon icon) {
            if (_next == GamePhaseIcon.None || _step == 0)
                _next = icon;
            else
                _next2 = icon;
        }

        public void DrawSpecial() {
            Rectangle rect = Rect;
            if (_step == 0) {
                if (_current != GamePhaseIcon.None)
                    PhasesSprite.Draw((int) _current, rect.Location);
                else
                    Display.FillRect(rect, Background);
                return;
            }
            Rectangle src, dest;
            dest = new Rectangle(rect.Left, rect.Top, rect.Width, rect.Height - _step);
            if (_current != GamePhaseIcon.None) {
                src = PhasesSprite.Descriptor.GetRect((int) _current);
                src.Y += _step;
                src.Height -= _step;
                PhasesSprite.Surface.Draw(src, dest);
            } else
                Display.FillRect(dest, Background);
            dest = new Rectangle(rect.Left, rect.Bottom - _step, rect.Width, _step);
            if (_next != GamePhaseIcon.None) {
                src = PhasesSprite.Descriptor.GetRect((int) _next);
                src.Height = _step;
                PhasesSprite.Surface.Draw(src, dest);
            } else
                Display.FillRect(dest, Background);
        }

        public override void OnProcess() {
            if (!HasNext && _step == 0) return;
            _step++;
            if (_step >= _maxStep) {
                NextIcon();
                _step = 0;
            }
        }

        private bool HasNext { get { return _next != GamePhaseIcon.None; } }

        private void NextIcon() {
            _current = _next;
            _next = _next2;
            _next2 = GamePhaseIcon.None;
        }

        public override void OnClick() {
            Data.Sounds.Events.Play("click");
            if (!Game.Current.IsMainCurrent) return;
            Game.Current.EndTurnClick();
        }
    }
}
